package com.cwt_taxi_xa;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cwt_taxi_xa.db.bll.ContactBiz;
import com.cwt_taxi_xa.model.ContactInfo;

import java.util.ArrayList;

public class ContactAdapter extends BaseAdapter{

	private LayoutInflater inflater;
	private ArrayList<ContactInfo> contacts;
	private ContactBiz biz;

	public void setContacts(ArrayList<ContactInfo> contacts) {
		if (contacts != null)
			this.contacts = contacts;
		else
			this.contacts = new ArrayList<ContactInfo>();
	}

	public ContactAdapter(Activity context,ArrayList<ContactInfo> contacts) {
		super();
		this.setContacts(contacts);
		this.inflater = LayoutInflater.from(context);
		biz = new ContactBiz(context);
	}

	public void changeData(ArrayList<ContactInfo> list){
		this.contacts = list;
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return contacts.size();
	}

	@Override
	public ContactInfo getItem(int position) {
		// TODO Auto-generated method stub
		return contacts.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;
		if(convertView == null){
			convertView = inflater.inflate(R.layout.item_contact, null);
			viewHolder = new ViewHolder();
			viewHolder.img= (ImageView) convertView.findViewById(R.id.img);
			viewHolder.name = (TextView) convertView.findViewById(R.id.name);
			viewHolder.number = (TextView) convertView.findViewById(R.id.number);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
		}

		/**设置listview参数*/
		ContactInfo contact = contacts.get(position);
		viewHolder.name.setText(contact.getName());
		viewHolder.number.setText(contact.getPhone());

		return convertView;
	}

	class ViewHolder{
		private ImageView img;
		private TextView name;
		private TextView number;
	}
}
